﻿$(function () {
    $.kish.carAnnAddEdit = {};
    $.extend($.kish.carAnnAddEdit, {

        //********************************//
        //set basid details
        carAnnID: null,
        carStoreID: null,
        carID: null,
        initializing: function () {
            $.kish.carAnnAddEdit.SetBrandDrp();
            $.kish.carAnnAddEdit.SetColorDrp();
            $.kish.carAnnAddEdit.SetStatusDrp();

            var carAnnID = $.Jahanutility.getParameterByName("ID");
            if (carAnnID != "0" && carAnnID != '') {
                $.kish.carAnnAddEdit.carAnnID = carAnnID;
                $.kish.carAnnAddEdit.SetEditPage(carAnnID);
            }
            else {
                var carStoreID = $.Jahanutility.getParameterByName("carStoreID");
                var carID = $.Jahanutility.getParameterByName("carID");
                if (carStoreID != "0" && carStoreID != '') {
                    $.kish.carAnnAddEdit.carStoreID = $.Jahanutility.getParameterByName("carStoreID");
                }
                if (carStoreID != "0" && carStoreID != '' && carID != "0" && carID != '') {                    
                    $.kish.carAnnAddEdit.carID = $.Jahanutility.getParameterByName("carID");
                    $.kish.carAnnAddEdit.SetCarStoreEditPage();
                }
            }

            $(".carAnnSubmit").click(function () {
                if ($.kish.carAnnAddEdit.carAnnID != null)
                    $.kish.carAnnAddEdit.AddCarAnn();
                else if ($.kish.carAnnAddEdit.carStoreID != null)
                    $.kish.carAnnAddEdit.AddCarInCarStore();
            });

            $('.brandDrp').change(function () {
                $('.modelDrp').html('');
                var brandID = $(this).val();
                if (brandID != 0)
                    $.kish.carAnnAddEdit.SetModelDrp(brandID);
            });
        },

        //********************************//
        SetBrandDrp: function () {
            var brandDrp = $('.brandDrp');
            var url = '../../KishCar/Services/CarAnn.svc/GetAllBrands';
            var data = '';
            var topOption = 'انتخاب کنید';
            $.Jahanutility.fillDrp(brandDrp, url, data, topOption);
        },

        SetModelDrp: function (ID, ModelID) {
            var brandDrp = $('.modelDrp');
            var url = '../../KishCar/Services/CarAnn.svc/GetModel';
            var data = JSON.stringify({ modelID: ID });
            var topOption = 'انتخاب کنید';
            $.Jahanutility.fillDrp(brandDrp, url, data, topOption, ModelID);
        },

        SetColorDrp: function (ID) {
            var brandDrp = $('.colorDrp');
            var url = '../../KishCar/Services/CarAnn.svc/GetColor';
            var data = '';
            var topOption = '---';
            $.Jahanutility.fillDrp(brandDrp, url, data, topOption);
        },

        SetStatusDrp: function () {
            var brandDrp = $('.stateDrp');
            var url = '../../KishCar/Services/CarAnn.svc/GetAllStatus';
            var data = '';
            var topOption = '';
            $.Jahanutility.fillDrp(brandDrp, url, data, topOption);
        },
        //****************************//
        AddCarAnn: function () {

            //start--------this part belongs to validation code  "focusout" helps to find empty inputs----------//
            // Kick in Validation
            $('.drpValidator').each(function () {
                $(this).triggerHandler("focusout");
            });

            $('.isNumeric').each(function () {
                $(this).triggerHandler("focusout");
            });

            if ($('#contact .error').size() > 0) {
                $('#contact').effect('shake', { times: 3 }, 50);
                return false;
            }
            //end--------this part belongs to validation code  "focusout" helps to find empty inputs-----------//


            $('#submit').before('<img src="../../images/ajax-loader.gif" class="loader" />')

            var carAnn = {};
            carAnn.Car = {};
            var carAnnID = $.Jahanutility.getParameterByName("ID");
            if (carAnnID != "0" && carAnnID != '') {
                carAnn.CarID = carAnnID;
            };
            if ($.trim($(".priceTxt").val()) != '') {
                carAnn.Car.Price = $.trim($(".priceTxt").val());
                carAnn.Car.Price = Number(carAnn.Car.Price.replace(/[^0-9\.]+/g, ""));
            }
            else {
                carAnn.Car.Price = null;
            }
            carAnn.Car.ProductYear = parseInt($.trim($(".yearTxt").val()), 10);
            carAnn.Car.TraveledDistance = parseInt($(".usageTxt").val(), 10);
            carAnn.Car.Description = $.trim($(".commentTxt").val());
            if ($(".colorDrp").val() != 0) {
                carAnn.Car.ColorID = parseInt($(".colorDrp").val(), 10);
            }
            else {
                carAnn.Car.ColorID = null;
            }
            carAnn.Car.ModelID = $.trim($(".modelDrp").val());
            if ($(".stateDrp").val() != 0) {
                carAnn.Car.StatusID = parseInt($(".stateDrp").val(), 10);
            }
            //carAnn.Car.StatusID = $.trim($(".stateDrp").val());
            carAnn.Car.KeyWords = $.trim($(".keyWordTxt1").val())
                                  + "," + $.trim($(".keyWordTxt2").val())
                                  + "," + $.trim($(".keyWordTxt3").val())
                                  + "," + $.trim($(".keyWordTxt4").val())
                                  + "," + $.trim($(".keyWordTxt5").val());

            carAnn.CarAnnouncementTariffID = 1; //$(".AnnTraffDrp").val();

            $('#submit').attr('disabled', 'disabled');
            $.ajax({
                'type': 'POST',
                'url': '../../KishCar/Services/CarAnn.svc/AddCarAnn',
                'data': JSON.stringify({ CarAnn: carAnn }),
                'contentType': 'application/json; charset=utf-8',
                'dataType': 'json',
                'success': function (result) {
                    if (result['d'].IsSucceed) {
                        $.kish.message.showUserMessage(result['d'].Message.Message);
                        window.location.href = 'CarAnnList.aspx';
                    }
                    else {
                        $.kish.message.showErrorMessage(result['d'].Message.Message);
                    };
                    $('#message').slideDown();
                    $('img.loader').fadeOut('slow', function () { $(this).remove() });
                    $('#submit').removeAttr('disabled', 'disabled');
                },
                'error': function (error) {
                    $('#submit').removeAttr('disabled');
                    $('img.loader').fadeOut('slow', function () { $(this).remove() });
                }
            });
        },
        SetEditPage: function (carAnnID) {
            $.ajax({
                'type': 'POST',
                'url': '../../KishCar/Services/CarAnn.svc/GetCarAnn',
                'data': JSON.stringify({ ID: carAnnID }),
                'contentType': 'application/json; charset=utf-8',
                'dataType': 'json',
                'async': false,
                'success': function (result) {
                    if (result['d'].IsSucceed) {
                        //$.kish.message.showUserMessage(result['d'].Message.Message);
                        var carAnn = result['d'].Data;
                        $(".priceTxt").val(carAnn.Car.Price);
                        $(".yearTxt").val(carAnn.Car.ProductYear);
                        $(".usageTxt").val(carAnn.Car.TraveledDistance);
                        $(".commentTxt").val(carAnn.Car.Description);
                        $.kish.carAnnAddEdit.SetModelDrp(carAnn.Car.Model.BrandID, carAnn.Car.Model.ID);
                        $(".colorDrp").val(carAnn.Car.ColorID);
                        $(".brandDrp").val(carAnn.Car.Model.BrandID);
                        $(".stateDrp").val(carAnn.Car.StatusID);
                        if (Car.KeyWords != null) {
                            $(".keyWordTxt1").val(carAnn.Car.KeyWords.split(",")[0]);
                            $(".keyWordTxt2").val(carAnn.Car.KeyWords.split(",")[1]);
                            $(".keyWordTxt3").val(carAnn.Car.KeyWords.split(",")[2]);
                            $(".keyWordTxt4").val(carAnn.Car.KeyWords.split(",")[3]);
                            $(".keyWordTxt5").val(carAnn.Car.KeyWords.split(",")[4]);
                        }
                    }
                    else {
                        $.kish.message.showErrorMessage(result['d'].Message.Message);
                    };
                    $('#message').slideDown();
                    $('img.loader').fadeOut('slow', function () { $(this).remove() });
                    $('#submit').removeAttr('disabled', 'disabled');
                },
                'error': function (error) {
                    $('#submit').removeAttr('disabled');
                    $('img.loader').fadeOut('slow', function () { $(this).remove() });
                }
            });
        },
        AddCarInCarStore: function () {
            //start--------this part belongs to validation code  "focusout" helps to find empty inputs----------//
            // Kick in Validation
            $('.drpValidator').each(function () {
                $(this).triggerHandler("focusout");
            });

            $('.isNumeric').each(function () {
                $(this).triggerHandler("focusout");
            });

            if ($('#contact .error').size() > 0) {
                $('#contact').effect('shake', { times: 3 }, 50);
                return false;
            }
            //end--------this part belongs to validation code  "focusout" helps to find empty inputs-----------//


            $('#submit').before('<img src="../../images/ajax-loader.gif" class="loader" />')

            var Car = {};
            if ($.kish.carAnnAddEdit.carID != null) {
                Car.ID = $.kish.carAnnAddEdit.carID;
            }
            else {
                Car.ID = 0;
            }
            if ($.trim($(".priceTxt").val()) != '') {
                Car.Price = $.trim($(".priceTxt").val());
                Car.Price = Number(Car.Price.replace(/[^0-9\.]+/g, ""));
            }
            else {
                Car.Price = null;
            }
            Car.ProductYear = parseInt($.trim($(".yearTxt").val()), 10);
            Car.TraveledDistance = parseInt($(".usageTxt").val(), 10);
            Car.Description = $.trim($(".commentTxt").val());
            if ($(".colorDrp").val() != 0) {
                Car.ColorID = parseInt($(".colorDrp").val(), 10);
            }
            else {
                Car.ColorID = null;
            }
            Car.ModelID = $.trim($(".modelDrp").val());
            if ($(".stateDrp").val() != 0) {
                Car.StatusID = parseInt($(".stateDrp").val(), 10);
            }
            //carAnn.Car.StatusID = $.trim($(".stateDrp").val());
            Car.KeyWords = $.trim($(".keyWordTxt1").val())
                                  + "," + $.trim($(".keyWordTxt2").val())
                                  + "," + $.trim($(".keyWordTxt3").val())
                                  + "," + $.trim($(".keyWordTxt4").val())
                                  + "," + $.trim($(".keyWordTxt5").val());

            $('#submit').attr('disabled', 'disabled');

            $.ajax({
                'type': 'POST',
                'url': '../../KishCar/Services/CarAnn.svc/AddCarInCarStore',
                'data': JSON.stringify({ CarStoreID: $.kish.carAnnAddEdit.carStoreID, Car: Car }),
                'contentType': 'application/json; charset=utf-8',
                'dataType': 'json',
                'success': function (result) {
                    if (result['d'].IsSucceed) {
                        $.kish.message.showUserMessage(result['d'].Message.Message);
                        window.location.href = 'EditCarStore.aspx?ID=' + $.kish.carAnnAddEdit.carStoreID.toString();
                    }
                    else {
                        $.kish.message.showErrorMessage(result['d'].Message.Message);
                    };
                    $('#message').slideDown();
                    $('img.loader').fadeOut('slow', function () { $(this).remove() });
                    $('#submit').removeAttr('disabled', 'disabled');
                },
                'error': function (error) {
                    $('#submit').removeAttr('disabled');
                    $('img.loader').fadeOut('slow', function () { $(this).remove() });
                }
            });
        },
        SetCarStoreEditPage: function () {
            $.ajax({
                'type': 'POST',
                'url': '../../KishCar/Services/CarAnn.svc/GetCarInCarStore',
                'data': JSON.stringify({ CarStoreID: $.kish.carAnnAddEdit.carStoreID, CarID: $.kish.carAnnAddEdit.carID }),
                'contentType': 'application/json; charset=utf-8',
                'dataType': 'json',
                'async': false,
                'success': function (result) {
                    if (result['d'] != null) {
                        //$.kish.message.showUserMessage(result['d'].Message.Message);
                        var Car = result['d'];
                        $(".priceTxt").val(Car.Price);
                        $(".yearTxt").val(Car.ProductYear);
                        $(".usageTxt").val(Car.TraveledDistance);
                        $(".commentTxt").val(Car.Description);
                        $.kish.carAnnAddEdit.SetModelDrp(Car.Model.BrandID, Car.Model.ID);
                        $(".colorDrp").val(Car.ColorID);
                        $(".brandDrp").val(Car.Model.BrandID);
                        $(".stateDrp").val(Car.StatusID);
                        if (Car.KeyWords != null) {
                            $(".keyWordTxt1").val(Car.KeyWords.split(",")[0]);
                            $(".keyWordTxt2").val(Car.KeyWords.split(",")[1]);
                            $(".keyWordTxt3").val(Car.KeyWords.split(",")[2]);
                            $(".keyWordTxt4").val(Car.KeyWords.split(",")[3]);
                            $(".keyWordTxt5").val(Car.KeyWords.split(",")[4]);
                        }
                    }
                    $('#message').slideDown();
                    $('img.loader').fadeOut('slow', function () { $(this).remove() });
                    $('#submit').removeAttr('disabled', 'disabled');
                },
                'error': function (error) {
                    $('#submit').removeAttr('disabled');
                    $('img.loader').fadeOut('slow', function () { $(this).remove() });
                }
            });
        }
    });
    $.kish.carAnnAddEdit.initializing();
});



